﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.ModelInterface;
using QLBH.DataModel;
using QLBH.Model.Controller;

namespace QLBH.FeatureModel
{
    public class HD_MatHangModel:IHD_MatHangModel
    {
        public Guid HDBHID { set; get; }        

        public Guid MHID {set;get;}
        public string MaMH { set; get; }


        public string TenMH { set; get; }

        public string DVT { set; get; }
        
        private int soluong;
        public int SOLUONG
        {
            get
            {
                return soluong;
            }
            set
            {
                soluong = value;
                thanhtien = GIABAN * soluong - chietkhau;
            }
        }
        private decimal giaban;
        public decimal GIABAN 
        { 
            set
            {
                giaban = value;
                thanhtien = giaban * soluong - chietkhau;
            }
            get
            {
                return giaban;
            }
        }

        private decimal chietkhau;
        public decimal CHIETKHAU
        {
            get
            {
                return chietkhau;
            }
            set
            {
                chietkhau = value;
                thanhtien = GIABAN * soluong - chietkhau;
            }
        }

        private decimal thanhtien;
        public decimal THANHTIEN
        {
            get
            {
                return thanhtien;
            }
            set
            {
                thanhtien = value;
            }
        }

        public string GHICHU { set; get; }

        public Guid KHOID { set; get; }
        

        public void InitFromEntity(DataModel.HDBHCHITIET entity)
        {
            MHID = entity.MHID;
            MATHANG mathang = MatHangController.GetInstance().FindBy(mh => mh.MHID == entity.MHID).FirstOrDefault();
            MaMH = mathang.MHCODE;
            TenMH = mathang.MHTEN;
            GIABAN = (decimal)entity.GIABAN;
            SOLUONG = (int)entity.SOLUONG;
            DONVITINH dvtobj = DVTController.GetInstance().FindBy(dvt => dvt.DVTID == mathang.DVTID).FirstOrDefault();
            if (dvtobj != null)
            {
                DVT = dvtobj.TENDVT;
            }
            CHIETKHAU = (decimal)entity.CHIETKHAU;

            KHOID = entity.KHOID;
        }

        public DataModel.HDBHCHITIET ToEntity()
        {
            HDBHCHITIET entity = new HDBHCHITIET();
            entity.CHIETKHAU = (decimal)this.CHIETKHAU;
            entity.GIABAN = this.GIABAN;
            entity.MHID = this.MHID;
            entity.SOLUONG = this.SOLUONG;
            entity.THANHTIEN = this.THANHTIEN;

            entity.KHOID = KHOID;
            entity.HDBHID = Guid.NewGuid();
            entity.GHICHU = GHICHU;
            return entity;
        }


        public void InitFromMatHangModel(MATHANG mh,bool banle)
        {
            MHID = mh.MHID;
            MaMH = mh.MHCODE;
            TenMH = mh.MHTEN;
            GIABAN = banle == true ? (decimal)mh.GIABANLE : (decimal)mh.GIABANBUON;
            SOLUONG = 0;
            DONVITINH dvtobj = DVTController.GetInstance().FindBy(dvt => dvt.DVTID == mh.DVTID).FirstOrDefault();
            if (dvtobj != null)
            {
                DVT = dvtobj.TENDVT;
            }
            CHIETKHAU = (decimal)NCC_MatHangController.GetInstance().FindBy(nccmh => nccmh.MHID == mh.MHID).FirstOrDefault().CHIETKHAU;
        }


        
        
    }
}
